<template>
	<view>
		<view class="search-box">
      <!-- 基本用法 -->
      <uni-search-bar @input="search" :radius="18" cancelButton="none" ></uni-search-bar>
		</view>
    <view class="sugg-list" v-if="searchResults.length !==0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i">
        <view class="good-name" @click="gotoDetail(item.goods_id)">{{item.goods_name}}</view>
        <uni-icons type="right" size="16"></uni-icons>
      </view>
    </view>
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" @click="cleanHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" type="primary" v-for="(item,i) in histortList" :key="i" />
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,//防抖延时器
        kv:'',
        searchResults:[],
        histortList:[]
			};
		},
    onLoad() {
      this.histortList = JSON.parse(uni.getStorageSync('kw') || '[]') 
    },
    methods:{
      cleanHistory(){
        uni.setStorageSync('kv','[]');
        this.histortList=[];
      },
      gotoDetail(goodsId){
        uni.navigateTo({
          url:'../goods_detail/goods_detail?goods_id='+goodsId
        })
      },
      search(e){
        clearTimeout(this.timer);
        this.timer = setTimeout(() =>{
          this.kv = e.value;
          this.getSearchList();
        },500)
      },
      async getSearchList(){
        if(this.kv === '') {
          this.searchResults = [];
          return;
        }
         const{data: res}  = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kv });
         if (res.meta.status !== 200) return uni.$showMsg();
         this.searchResults = res.message;
         this.saveHistory();
         uni.setStorageSync('kv',JSON.stringify(this.histortList));
      },
      saveHistory(){
        //this.histortList.push(this.kv);
        const set = new Set(this.histortList);
        set.delete(this.kv);
        set.add(this.kv);
        this.histortList = Array.from(set);
      }
    },
    computed:{
      histories(){
        return [...this.histortList].reverse();
      }
    }
	}
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list {
  padding: 0 5px;

  .sugg-item {
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}

.history-box{
  margin: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
   
    .uni-tag{
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}

</style>
